package com.rran.study.designpattern.factory.singleton.hungry;

/**
 * @author yy
 * @Type main.java
 * @Desc 饿汉单例 推荐使用
 * @date 2020/7/22 15:41
 */
public class HungrySingleton {

    private HungrySingleton() {
        if (instance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    private static final HungrySingleton instance = new HungrySingleton();

    public static HungrySingleton getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                HungrySingleton instance = HungrySingleton.getInstance();
                System.out.println(Thread.currentThread().getName()+" instance hashcode:"+instance.hashCode());
            },String.valueOf(i)).start();
        }
    }

}
